#!/bin/bash
#author desenix@gmail.com
clear
# -=== INIT SYS VAR ===-
locdir=`dirname $0`
locfile=`basename $0`
# -=== GLOBAL VAR ===-
sysconfig="/etc/sysconfig/network-scripts"
IPT=/sbin/iptables
dlog="$locdir/log"
flog="$dlog/netscr.log"
tlog="$dlog/temp.log"
dcache="$locdir/cache"
dcfg="$locdir/cfg"
drun="$locdir/run"
# -=== LINK CONTROL ===-
lcpause=3
#==============================================
# for Result -------
declare -a ares
res=""
#-------------------
if [ -f $flog ]; then
tail -500 $flog > $tlog
mv $tlog $flog
fi
ptime=`date "+%F %u %T %Z"`
exec 1>> $flog
exec 2>> $flog
# -=== START ===-
echo ">>> $ptime => $@"

#==============================================
GetIfParam()
{
local declare pif=$1
local declare vif=$2
local declare aip
local declare amsk
local declare ip
local declare net
local declare msk
local declare hw
local declare tlnk
local declare lnk
local declare srv
local declare tmp

ares=( )

if [ "$pif" == "" ]; then
echo "ERROR: GetIfParam arg[0]=NUL"
exit 0
fi

tmp=`ifconfig $pif | sed '$!N;s/\n/ /'`
echo $tmp | grep -q "$pif" || exit 0
ip=`echo $tmp | sed -n 's/.*inet addr:\([0-9.]*\).*/\1/gp'`
srv=`echo $tmp | sed -n 's/.*P-t-P:\([0-9.]*\).*/\1/gp'`
msk=`echo $tmp | sed -n 's/.*Mask:\([0-9.]*\).*/\1/gp'`
hw=`echo $tmp | sed -n 's/.*HWaddr \([0-9A-Fa-f:]*\).*/\1/gp'`
lnk=`echo $tmp | grep -c " UP "`
tlnk=`echo $tmp | sed -n 's/.*Link encap:\([^ ]*\).*/\1/gp'`
aip=( $( echo $ip | sed 's/\./ /g' ) )
amsk=( $( echo $msk | sed 's/\./ /g' ) )
net=( $(echo $((${aip[0]}&${amsk[0]})).$((${aip[1]}&${amsk[1]})).$((${aip[2]}&${amsk[2]})).$((${aip[3]}&${amsk[3]}))/$msk ) )

echo ip=$ip > $dcache/$pif.cfg
echo msk=$msk >> $dcache/$pif.cfg
echo net=$net >> $dcache/$pif.cfg
echo hw=$hw >> $dcache/$pif.cfg
echo tlnk=$tlnk >> $dcache/$pif.cfg
echo lnk=$lnk >> $dcache/$pif.cfg
echo srv=$srv >> $dcache/$pif.cfg
echo pif=$pif >> $dcache/$pif.cfg
echo vif=$vif >> $dcache/$pif.cfg

if [ "$vif" != "" -a "$vif" != "$pif" ]; then
echo ip=$ip > $dcache/$vif.cfg
echo msk=$msk >> $dcache/$vif.cfg
echo net=$net >> $dcache/$vif.cfg
echo hw=$hw >> $dcache/$vif.cfg
echo tlnk=$tlnk >> $dcache/$vif.cfg
echo lnk=$lnk >> $dcache/$vif.cfg
echo srv=$srv >> $dcache/$vif.cfg
echo pif=$pif >> $dcache/$vif.cfg
echo vif=$vif >> $dcache/$vif.cfg
fi

ares=( [0]="$ip" [1]="$msk" [2]="$net" [3]="$hw" [4]="$tlnk" [5]="$lnk" [6]="$srv" [7]="$pif" [8]="$vif" )
}
#==============================================
LoadIfParam()
{
local declare ip
local declare net
local declare msk
local declare hw
local declare lnk
local declare tlnk
local declare srv

. $dcache/$1.cfg

ares=( [0]="$ip" [1]="$msk" [2]="$net" [3]="$hw" [4]="$tlnk" [5]="$lnk" [6]="$srv" [7]="$pif" [8]="$vif" )
}
#==============================================
GetDefaultGw()
{
local declare atmp
res=""
atmp=( $( traceroute -nm 1 $1 2>/dev/nul | grep -v traceroute ) )
res=${atmp[1]}
#echo "res=$res"
}
#==============================================
NLS-IPT-START()
{
local declare var_ifp=lo
local declare var_ifv=lo
local declare var_ip=127.0.0.1
local declare var_mask=255.0.0.0
local declare var_net=127.0.0.0/8


$IPT -F
$IPT -t nat -F
$IPT -t mangle -F

$IPT -X
$IPT -t nat -X
$IPT -t mangle -X

$IPT -P INPUT DROP
$IPT -P OUTPUT DROP
$IPT -P FORWARD DROP

$IPT -N st_tcp_accept
$IPT -A st_tcp_accept -p tcp --syn -j ACCEPT
$IPT -A st_tcp_accept -p tcp -m state --state ESTABLISHED,RELATED -j ACCEPT
$IPT -A st_tcp_accept -p tcp -j DROP

$IPT -N st_drop
$IPT -A st_drop -j DROP

$IPT -N st_tcp_test
$IPT -A st_tcp_test -p tcp --tcp-flags SYN,ACK SYN,ACK -m state --state NEW -j REJECT --reject-with tcp-reset
$IPT -A st_tcp_test -p tcp ! --syn -m state --state NEW -j DROP

$IPT -N st_udp_test

$IPT -N st_icmp_test_i
$IPT -A st_icmp_test_i --fragment -p icmp -j DROP
$IPT -A st_icmp_test_i -p icmp -s 0/0 --icmp-type source-quench -j ACCEPT
$IPT -A st_icmp_test_i -p icmp -s 0/0 --icmp-type echo-reply -j ACCEPT
$IPT -A st_icmp_test_i -p icmp -s 0/0 --icmp-type echo-request -j ACCEPT
$IPT -A st_icmp_test_i -p icmp -s 0/0 --icmp-type 12 -j ACCEPT #parametr-problem

$IPT -N st_icmp_test_o
$IPT -A st_icmp_test_o --fragment -p icmp -j DROP
$IPT -A st_icmp_test_o -p icmp -d 0/0 --icmp-type source-quench -j ACCEPT
$IPT -A st_icmp_test_o -p icmp -d 0/0 --icmp-type echo-request -j ACCEPT
$IPT -A st_icmp_test_o -p icmp -d 0/0 --icmp-type echo-reply -j ACCEPT
$IPT -A st_icmp_test_o -p icmp -d 0/0 --icmp-type 12 -j ACCEPT #parametr-problem

$IPT -A INPUT	-m state --state INVALID -j DROP
$IPT -A INPUT	-p tcp -j st_tcp_test
$IPT -A INPUT	-m state --state ESTABLISHED,RELATED -j ACCEPT
#$IPT -A INPUT	! --syn -m state --state NEW -j DROP
$IPT -A INPUT	-p icmp -i lo -j st_icmp_test_i
##PT -A INPUT	-p ALL  -i lo -s $var_net -d $var_net -j ACCEPT
$IPT -A INPUT	-p ALL  -i lo -j ACCEPT

$IPT -A OUTPUT	-m state --state INVALID -j DROP
#$IPT -A OUTPUT	-p icmp -o lo -j st_icmp_test_o
##$IPT -A OUTPUT	-p ALL  -o lo -s $var_net -d $var_net -j ACCEPT
$IPT -A OUTPUT	-p ALL  -o lo -j ACCEPT

$IPT -A FORWARD	-m state --state INVALID -j DROP
$IPT -A FORWARD	-p tcp -j st_tcp_test
$IPT -A FORWARD	-m state --state ESTABLISHED,RELATED -j ACCEPT
$IPT -A FORWARD	-p ALL  -i lo -j ACCEPT
#$IPT -A FORWARD	! --syn -m state --state NEW -j DROP
#$IPT -A FORWARD	-p icmp -i lo -j st_icmp_test_i

}
#==============================================
NLS-IPT-STOP()
{
$IPT -P INPUT ACCEPT
$IPT -P OUTPUT ACCEPT
$IPT -P FORWARD ACCEPT
$IPT -t nat -P PREROUTING ACCEPT
$IPT -t nat -P POSTROUTING ACCEPT
$IPT -t nat -P OUTPUT ACCEPT
$IPT -t mangle -P PREROUTING ACCEPT
$IPT -t mangle -P OUTPUT ACCEPT
$IPT -F
$IPT -t nat -F
$IPT -t mangle -F
$IPT -X
$IPT -t nat -X
$IPT -t mangle -X
}
#==============================================
#==============================================
NLS-IPT-IFUP()
{
local declare var_ifp=$1
local declare var_ifv=$2
local declare var_ip=$3
local declare var_mask=$4
local declare var_net=$5
local declare var_srv=$6
local declare var_iflo=lo
local declare var_netlo=127.0.0.0/8
local declare fcfg=$dcfg/$var_ifv.scr

echo IFUP $1 $2 $3 $4 $5
#--------------------------
# доверенная локальная сеть
SETMODELAN()
{
$addfltinp -p ALL -i $var_ifp  -s $var_net -j ACCEPT
$addfltfwd -p ALL -i $var_ifp  -s $var_net -j ACCEPT

echo "MODE LAN"
}
#--------------------------
# чужая маршрутизируемая локальная сеть
SETMODELVS()
{
echo "MODE LVS"
}
#--------------------------
# внешняя сеть с доступом через NAT
SETMODEWAN()
{

# NAT
$addnatpst -o $var_ifp -j SNAT --to-source $var_ip

# заблокируем обращения к ЛВС через маршрут по умолчанию.
# $addfltout -p ALL -o $var_ifp -s $var_ip -d 0.0.0.0/8 -d 1.0.0.0/8 -d 2.0.0.0/8 -d 5.0.0.0/8 -d 10.0.0.0/8 -d 23.0.0.0/8 -d 27.0.0.0/8 -d 31.0.0.0/8 -d 36.0.0.0/8 -d 37.0.0.0/8 -d 39.0.0.0/8 -d 41.0.0.0/8 -d 42.0.0.0/8 -d 58.0.0.0/8 -d 59.0.0.0/8 -d 60.0.0.0/8 -d 70.0.0.0/7 -d 72.0.0.0/5 -d 82.0.0.0/7 -d 84.0.0.0/6 -d 88.0.0.0/5 -d 96.0.0.0/4 -d 112.0.0.0/5 -d 120.0.0.0/6 -d 124.0.0.0/7 -d 126.0.0.0/8 -d 127.0.0.0/8 -d 128.0.0.0/16 -d 128.66.0.0/16 -d 169.0.0.0/7 -d 169.254.0.0/16 -d 172.16.0.0/12 -d 191.255.0.0/16 -d 192.0.0.0/16 -d 192.88.99.0/24 -d 192.168.0.0/16 -d 197.0.0.0/8 -d 198.18.0.0/15 -d 201.0.0.0/8 -d 222.0.0.0/7 -d 224.0.0.0/4 -d 240.0.0.0/4 -d 255.255.255.255/32 -j REJECT icmp-net-unreachable

echo "MODE WAN"
}
#--------------------------
OPENTCPPORT()
{
$addfltinp -p TCP -i $var_ifp -m multiport --destination-port $1 -j ACCEPT
}
#--------------------------
OPENUDPPORT()
{
$addfltinp -p UDP -i $var_ifp -m multiport --destination-port $1 -j ACCEPT
}
#--------------------------
FORWARDTCPPORT()
{
$addnatpre -p TCP -m multiport --destination-port $1 -j DNAT --to-destination $2
}
#--------------------------
FORWARDUDPPORT()
{
$addnatpre -p UDP -m multiport --destination-port $1 -j DNAT --to-destination $2
}
#--------------------------
# добавление маршрута к серверу и маршрута по умолчанию
PPPADDROUTE()
{
echo route del -host $var_srv dev $var_ifp
route del -host $var_srv dev $var_ifp

GetDefaultGw $var_srv
echo $res

echo route add -host $var_srv gw $res
route add -host $var_srv gw $res

echo route add default dev $var_ifp
route add default dev $var_ifp

# сохраним добавленные маршруты для последующего удаления
echo "route del -host $var_srv gw $res" >> $dcache/$var_ifp.down
echo "route del default dev $var_ifp"   >> $dcache/$var_ifp.down
}
#--------------------------

# проверим наличие таблиц для запрошенного интерфейса
iptlink=`iptables -L | grep -c $var_ifp`
if [ "$iptlink" != "0" ]
then
echo "Iptables $var_ifp already UP"
exit 0
fi

### INPUT ###
# PREROUTING mangle
# PREROUTING nat
# *ROTING*inp*
# INPUT mangle
# INPUT filter

### FORWARD ###
# PREROUTING mangle
# PREROUTING nat
# *ROTING*inp*
# FORWARD mangle
# FORWARD filter
# *ROTING*out*
# POSTROUTING mangle
# POSTROUTING nat

### OUTPUT ###
# *ROTING*
# OUTPUT mangle
# OUTPUT nat
# OUTPUT filter
# *ROTING*out*
# POSTROUTING mangle
# POSTROUTING nat

### PREROUTING ###
# PREROUTING mangle
$IPT -t mangle -N st_mngpre_$var_ifp
$IPT -t mangle -A PREROUTING -i $var_ifp -j st_mngpre_$var_ifp
local declare addmngpre="$IPT -t mangle -A st_mngpre_$var_ifp"

# PREROUTING nat
$IPT -t nat -N st_natpre_$var_ifp
$IPT -t nat -A PREROUTING -i $var_ifp -j st_natpre_$var_ifp
local declare addnatpre="$IPT -t nat -A st_natpre_$var_ifp"

### INPUT ###
# INPUT mangle
$IPT -t mangle -N st_mnginp_$var_ifp
$IPT -t mangle -A INPUT -i $var_ifp -j st_mnginp_$var_ifp
local declare addmnginp="$IPT -t mangle -A st_mnginp_$var_ifp"

# INPUT filter
$IPT -N st_fltinp_$var_ifp
$IPT -A INPUT -i $var_ifp -j st_fltinp_$var_ifp
local declare addfltinp="$IPT -A st_fltinp_$var_ifp"

### FORWARD ###
# FORWARD mangle
$IPT -t mangle -N st_mngfwd_$var_ifp
$IPT -t mangle -A FORWARD -i $var_ifp -j st_mngfwd_$var_ifp
local declare addmngfwd="$IPT -t mangle -A st_mngfwd_$var_ifp"

# FORWARD filter
$IPT -N st_fltfwd_$var_ifp
$IPT -A FORWARD -i $var_ifp -j st_fltfwd_$var_ifp
local declare addfltfwd="$IPT -A st_fltfwd_$var_ifp"

### OUTPUT ###
# OUTPUT mangle
$IPT -t mangle -N st_mngout_$var_ifp
$IPT -t mangle -A OUTPUT -o $var_ifp -j st_mngout_$var_ifp
local declare addmngout="$IPT -t mangle -A st_mngout_$var_ifp"

# OUTPUT nat
$IPT -t nat -N st_natout_$var_ifp
$IPT -t nat -A OUTPUT -o $var_ifp -j st_natout_$var_ifp
local declare addnatout="$IPT -t nat -A st_natout_$var_ifp"

# OUTPUT filter
$IPT -N st_fltout_$var_ifp
$IPT -A OUTPUT -o $var_ifp -j st_fltout_$var_ifp
local declare addfltout="$IPT -A st_fltout_$var_ifp"

### POSTROUTING ###
# POSTROUTING mangle
$IPT -t mangle -N st_mngpst_$var_ifp
$IPT -t mangle -A POSTROUTING -o $var_ifp -j st_mngpst_$var_ifp
local declare addmngpst="$IPT -t mangle -A st_mngpst_$var_ifp"

# POSTROUTING nat
$IPT -t nat -N st_natpst_$var_ifp
$IPT -t nat -A POSTROUTING -o $var_ifp -j st_natpst_$var_ifp
local declare addnatpst="$IPT -t nat -A st_natpst_$var_ifp"

if [ -f $fcfg ]; then
. $fcfg
LINKUP
echo UP $fcfg
fi

$addfltinp -p icmp -j st_icmp_test_i
#$addfltout -p ALL -o $var_ifp  -s $var_ip  -j ACCEPT
$addfltout -p ALL -o $var_ifp  -j ACCEPT

$IPT -A INPUT   -p ALL -i $var_iflo -d $var_ip -j ACCEPT
#$IPT -A FORWARD -p ALL -i $var_iflo -d $var_ip -j ACCEPT
#$IPT -A OUTPUT  -p ALL -o $var_iflo -s $var_ip -j ACCEPT
$IPT -A OUTPUT  -p ALL -o $var_iflo -j ACCEPT

}
#==============================================
NLS-IPT-IFDOWN()
{
local declare var_ifp=$1
local declare var_ifv=$2
local declare var_ip=$3
local declare var_mask=$4
local declare var_net=$5
local declare var_srv=$6
local declare var_iflo=lo
local declare var_netlo=127.0.0.0/8
local declare fcfg=$dcfg/$var_ifp.scr
local declare iptlink

# проверим наличие таблиц для запрошенного интерфейса
iptlink=`iptables -L | grep -c $var_ifp`
if [ "$iptlink" == "0" ]; then
echo "Iptables $var_ifp already Down"
exit 0
fi

# вызов скрипта конфигурации пользователя
if [ -f $fcfg ]; then
. $fcfg
LINKDOWN
echo DOWN $fcfg
fi

$IPT -D INPUT   -p ALL -i $var_iflo -d $var_ip -j ACCEPT
$IPT -D OUTPUT  -p ALL -o $var_iflo -j ACCEPT
#$IPT -D FORWARD -p ALL -i $var_iflo -d $var_ip -j ACCEPT
#$IPT -D OUTPUT  -p ALL -o $var_iflo -s $var_ip -j ACCEPT

### PREROUTING ###
# PREROUTING mangle
$IPT -t mangle -D PREROUTING -i $var_ifp -j st_mngpre_$var_ifp
$IPT -t mangle -F st_mngpre_$var_ifp
$IPT -t mangle -X st_mngpre_$var_ifp

# PREROUTING nat
$IPT -t nat -D PREROUTING -i $var_ifp -j st_natpre_$var_ifp
$IPT -t nat -F st_natpre_$var_ifp
$IPT -t nat -X st_natpre_$var_ifp

### INPUT ###
# INPUT mangle
$IPT -t mangle -D INPUT -i $var_ifp -j st_mnginp_$var_ifp
$IPT -t mangle -F st_mnginp_$var_ifp
$IPT -t mangle -X st_mnginp_$var_ifp

# INPUT filter
$IPT -D INPUT -i $var_ifp -j st_fltinp_$var_ifp
$IPT -F st_fltinp_$var_ifp
$IPT -X st_fltinp_$var_ifp

### FORWARD ###
# FORWARD mangle
$IPT -t mangle -D FORWARD -i $var_ifp -j st_mngfwd_$var_ifp
$IPT -t mangle -F st_mngfwd_$var_ifp
$IPT -t mangle -X st_mngfwd_$var_ifp

# FORWARD filter
$IPT -D FORWARD -i $var_ifp -j st_fltfwd_$var_ifp
$IPT -F st_fltfwd_$var_ifp
$IPT -X st_fltfwd_$var_ifp

### OUTPUT ###
# OUTPUT mangle
$IPT -t mangle -D OUTPUT -o $var_ifp -j st_mngout_$var_ifp
$IPT -t mangle -F st_mngout_$var_ifp
$IPT -t mangle -X st_mngout_$var_ifp

# OUTPUT nat
$IPT -t nat -D OUTPUT -o $var_ifp -j st_natout_$var_ifp
$IPT -t nat -F st_natout_$var_ifp
$IPT -t nat -X st_natout_$var_ifp

# OUTPUT filter
$IPT -D OUTPUT -o $var_ifp -j st_fltout_$var_ifp
$IPT -F st_fltout_$var_ifp
$IPT -X st_fltout_$var_ifp

### POSTROUTING ###
# POSTROUTING mangle
$IPT -t mangle -D POSTROUTING -o $var_ifp -j st_mngpst_$var_ifp
$IPT -t mangle -F st_mngpst_$var_ifp
$IPT -t mangle -X st_mngpst_$var_ifp

# POSTROUTING nat
$IPT -t nat -D POSTROUTING -o $var_ifp -j st_natpst_$var_ifp
$IPT -t nat -F st_natpst_$var_ifp
$IPT -t nat -X st_natpst_$var_ifp

# вызов скрипта удаления изминений
if [ -f "$dcache/$var_ifp.down" ]; then
. $dcache/$var_ifp.down
rm $dcache/$var_ifp.down
echo "DOWN $dcache/$var_ifp.down"
fi
}
#==============================================
#==============================================
#==============================================
SysStart()
{
echo "Start system"
GetIfParam $iflo
iplo="${ares[0]}"
msklo="${ares[1]}"
netlo="${ares[2]}"
echo $iflo : $iplo : $msklo : $netlo
}
#==============================================
NLS-IPT-RESET()
{
NLS-IPT-STOP
NLS-IPT-START
}
#==============================================
IPTables()
{
case "$1" in
"start"		)	NLS-IPT-START;;
"stop"		)	NLS-IPT-STOP;;
"restart"	)	NLS-IPT-RESET;;
"reset"		)	NLS-IPT-RESET;;
esac
}
#==============================================
#		ETH
#==============================================
ETH-IPT-IFUP()
{
local declare var_ifp=$1
local declare var_ip=$3
local declare var_mask=$4
local declare var_net=$5
local declare var_mask
local declare var_net

GetIfParam $1

if [ "${ares[5]}" == "1" ]; then
var_ifp="$1"
var_ip="${ares[0]}"
var_mask="${ares[1]}"
var_net="${ares[2]}"
NLS-IPT-IFUP	$var_ifp $var_ifp $var_ip $var_mask $var_net
else
echo "ETH-IPT-IFUP Not UP"
fi
}
#==============================================
ETH-IPT-IFDN()
{
local declare var_ifp=$1
local declare var_ip=$3
local declare var_mask=$4
local declare var_net=$5
local declare var_mask
local declare var_net

LoadIfParam $1

# if [ "${ares[5]}" != "1" ]; then
var_ifp="$1"
var_ip="${ares[0]}"
var_mask="${ares[1]}"
var_net="${ares[2]}"
NLS-IPT-IFDOWN	$var_ifp $var_ifp $var_ip $var_mask $var_net
# else
# echo "ETH-IPT-IFDN Not Down"
# fi
}
#==============================================
ETH-IPT-IFRST()
{
ETH-IPT-IFDN $1
ETH-IPT-IFUP $1
}
#==============================================
#		PPP
#==============================================
PPP-IPT-IFUP()
{
local declare var_ifp=$1
local declare var_ifv=$6
local declare var_ip=$4
local declare var_srv=$5
local declare var_mask
local declare var_net

GetIfParam $var_ifp $var_ifv

if [ "${ares[5]}" == "1" ]; then
#var_ifp="$1"
#var_ip="${ares[0]}"
var_mask="${ares[1]}"
var_net="${ares[2]}"
NLS-IPT-IFUP	$var_ifp $var_ifv $var_ip $var_mask $var_net $var_srv
else
echo "PPP-IPT-IFUP Not UP"
fi
}
#==============================================
PPP-IPT-IFDN()
{
local declare var_ifp=$1
local declare var_ifv=$6
local declare var_ip=$4
local declare var_srv=$5
local declare var_mask
local declare var_net

LoadIfParam $1

# if [ "${ares[5]}" != "1" ]; then
var_mask="${ares[1]}"
var_net="${ares[2]}"
NLS-IPT-IFDOWN	$var_ifp $var_ifv $var_ip $var_mask $var_net $var_srv
# else
# echo "PPP-IPT-IFUP Not DOWN"
# fi
}
#==============================================
PPP-IPT-IFRST()
{
local declare var_ifp
local declare var_ifv
local declare var_ip
local declare var_srv
local declare var_mask
local declare var_net

LoadIfParam $1

var_ifp="${ares[7]}"
var_ifv="${ares[8]}"
var_ip="${ares[0]}"
var_srv="${ares[6]}"
var_mask="${ares[1]}"
var_net="${ares[2]}"

NLS-IPT-IFDOWN	$var_ifp $var_ifv $var_ip $var_mask $var_net $var_srv
NLS-IPT-IFUP	$var_ifp $var_ifv $var_ip $var_mask $var_net $var_srv
}
#==============================================
#==============================================
#		Link Control
#==============================================
LinkCntrl()
{
local declare if=$1
local declare phlink
local declare lglink
phlink=`ethtool $if 2>/dev/nul | grep -c "Link detected: yes"`
lglink=`ifconfig $if 2>/dev/nul | grep -c "UP "`
if [ "$phlink" != "$lglink" ]
then
#echo "Reset link $if -> $phlink"
if [ "$phlink" == "1" ]
then
echo "LinkControl IF=$if UP"
ifup $if
else
echo "LinkControl IF=$if DOWN"
ifdown $if
fi
fi
}
#----------------------------------------------
LinkControl()
{
local declare PID=""
local declare RUN=""
local declare frun="$drun/linkcontrol"
local declare FNAME=""

sleep 1

if [ -f "$frun" ]; then
PID=`cat $frun`
fi

#echo "PID=$PID FID=$locfile"

if [ "$PID" != "" ]
then
RUN=`ps -p $PID | grep -c $locfile`
fi

if [ "$RUN" == "1" ]
then
echo "LinkContril Already Run !!! STOP !!!"
exit 0
else
echo "LinkControl Start Thread"
echo "$$" > $frun
fi

RUN=1
#while true;
while [ "$RUN" == "1" ]
do

find "$dcfg" -type f | grep "eth" | while read FNAME; do
if=`echo $FNAME | sed -n 's/.*\(eth.*\).scr/\1/gp'`
ctrl=`grep -i "^linkcontrol" $FNAME | grep -c -i yes`
if [ "$ctrl" == "1" ]
then
LinkCntrl $if
fi
done

sleep $lcpause

if [ -f $frun ]; then
PID=`cat $frun`
if [ "$$" != "$PID" ]
then
RUN=""
fi
else
RUN=""
fi

#if [ "$RUN" != "1" ]
#then
#echo "LinkControl Stop Thread"
#sleep 1
#exit 0
#fi

done

echo "LinkControl Stop Thread"
}
#==============================================
LinkControlSw()
{
local declare frun="$drun/linkcontrol"
case "$1" in
"start"		)	$0 linkcontrol thread &;;
"stop"		)	echo > $frun;;
"restart"	)	echo > $frun;
			$0 linkcontrol thread &;;
"thread"	)	LinkControl;;
esac
}
#==============================================
InstallScr()
{
local declare scr=$1
local declare str=$2
grep -c "$str" "$scr" >/dev/nul 2>&1 && exit 0
echo $str >> $scr
chmod a+x $scr
}
#==============================================
UnInstallScr()
{
local declare scr=$1
local declare str=$2
grep -v "$str" "$scr" > $scr
}
#==============================================
Install()
{
echo "NetScr Install"
InstallScr "/etc/rc.d/rc.local.local" "/usr/scr/netscr iptables start"
InstallScr "/etc/rc.d/rc.local.local" "/usr/scr/netscr linkcontrol start"
InstallScr "/sbin/ifup-local" "/usr/scr/netscr ethup \$@"
InstallScr "/sbin/ifdown-local" "/usr/scr/netscr ethdn \$@"
InstallScr "/etc/ppp/ip-up.local" "/usr/scr/netscr pppup \$@"
InstallScr "/etc/ppp/ip-down.local" "/usr/scr/netscr pppdn \$@"
if [ ! -f "/usr/scr/netscr" ]; then
mkdir /usr/scr
mkdir /usr/scr/cache
mkdir /usr/scr/cfg
mkdir /usr/scr/log
mkdir /usr/scr/run
cp $0 /usr/scr/netscr
fi
echo "NetScr Install"
}
#==============================================
UnInstall()
{
echo "NetScr UnInstall"
UnInstallScr "/etc/rc.d/rc.local.local" "/usr/scr/netscr iptables start"
UnInstallScr "/etc/rc.d/rc.local.local" "/usr/scr/netscr linkcontrol start"
UnInstallScr "/sbin/ifup-local" "/usr/scr/netscr ethup"
UnInstallScr "/sbin/ifdown-local" "/usr/scr/netscr ethdn"
UnInstallScr "/etc/ppp/ip-up.local" "/usr/scr/netscr pppup"
UnInstallScr "/etc/ppp/ip-down.local" "/usr/scr/netscr pppdn"
rm -R /usr/scr
echo "NetScr UnInstall"
}
#==============================================
# -=== MAIN ===-
#==============================================
case "$1" in
"iptables"	)	IPTables $2;;
"ethup"		)	ETH-IPT-IFUP $2;;
"ethdn"		)	ETH-IPT-IFDN $2;;
"ethrst"	)	ETH-IPT-IFRST $2;;
"pppup"		)	shift;
			PPP-IPT-IFUP "$@";;
"pppdn"		)	shift;
			PPP-IPT-IFDN "$@";;
"ppprst"	)	shift;
			PPP-IPT-IFRST "$@";;
"linkcontrol"	)	LinkControlSw $2;;
"install"	)	Install;;
"uninstall"	)	UnInstall;;
esac
#==============================================
# -=== END ===-
